package com.syl.starter.itools.framework.utils;

import com.syl.starter.itools.base.beans.Response;
import com.syl.starter.itools.base.constants.BaseConstant;
import com.syl.starter.itools.base.enums.ResponseCodeEnum;
import com.syl.starter.itools.base.utils.LogUtils;
import com.syl.starter.itools.framework.enums.DownTypeEnum;
import com.syl.starter.itools.framework.enums.HttpContentTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Response 工具类
 *
 * @author syl
 * @create 2018-09-12 19:40
 **/
public class ResponseUtils {
    private static Logger LOG = LoggerFactory.getLogger(ResponseUtils.class);
    private static final String CACHE_CONTROL = "Cache-Control";
    private static final String CONTENT_LENGTH = "Content-Length";
    private static final String CONTENT_DISPOSITION = "Content-disposition";
    private static final String TEXT_HTML_CHARSET_UTF_8 = "text/html;charset=utf-8";

    /**
     * 请求是否成功
     * @return
     */
    public static boolean isSucceed(Response response){
        if(response != null && ResponseCodeEnum.OK.getCode().equals(response.getCode()))
            return true;
        return false;
    }

    /**
     * 输出json格式的数据
     * @param servletResponse
     * @param myResponse
     */
    public static void outputJson(ServletResponse servletResponse, Response myResponse) {
        try {
            servletResponse.setCharacterEncoding(BaseConstant.UTF8);
            servletResponse.setContentType(HttpContentTypeEnum.JSON.getValue());
            servletResponse.getWriter().println(JsonUtils.toJson(myResponse));
        } catch (IOException e) {
            LogUtils.error(LOG,"response输出json 出现io异常", e);
        }
    }

    /**
     * 设置不使用缓存
     * @param response
     */
    public static void setNoCache(HttpServletResponse response){
        response.setDateHeader("Expires", 0L);
        response.setHeader(CACHE_CONTROL, "no-store, no-cache, must-revalidate");
        response.addHeader(CACHE_CONTROL, "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
    }

    /**
     * 输出json格式的数据
     * @param response
     * @param msg
     * @param cors 是否跨域
     */
    @Deprecated
    public static void outputJson(HttpServletResponse response, Response msg, boolean cors) {
        try {
            if(cors)
                setCors(response);
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            response.getWriter().println(JsonUtils.toJson(msg));
        } catch (IOException e) {
            LOG.error("response输出json 出现io异常", e);
        }
    }

    /**
     * 设置下载头信息
     * @param response
     * @param downType 下载类型
     * @param downName 下载名称
     * @param size 大小 如未知大小 传null
     */
    public static void setDownLoadHead(HttpServletResponse response, DownTypeEnum downType, String downName, Long size){
        try {
            response.setCharacterEncoding(BaseConstant.UTF8);
            response.setContentType(TEXT_HTML_CHARSET_UTF_8);
            response.setContentType(downType.getContentType());
            response.setHeader(
                CONTENT_DISPOSITION,
                "attachment; filename="+ new String(downName.getBytes(BaseConstant.UTF8), BaseConstant.ISO8859_1)
            );
            if(size != null)
                response.setHeader(CONTENT_LENGTH, String.valueOf(size));
        } catch (UnsupportedEncodingException e) {
            LogUtils.error(LOG, "设置下载头信息错误", e);
        }
    }

    /**
     * 设置跨域 在这里仅能写死 该方法迁移到base-cli中
     * @param response
     */
    @Deprecated
    public static void setCors(HttpServletResponse response){
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Content-Type", "application/json");
    }

}
